package com.gwz;

import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

//演示例子，执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
public class CodeGenerator {

/**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
   Scanner scanner = new Scanner(System.in);
   StringBuilder help = new StringBuilder();
   help.append("请输入" + tip + "：");
   System.out.println(help.toString());
   if (scanner.hasNext()) {
       String ipt = scanner.next();
       if (StringUtils.isNotEmpty(ipt)) {
           return ipt;
       }
   }
   throw new MybatisPlusException("请输入正确的" + tip + "！");
}

public static void main(String[] args) {
   // 代码生成器
   AutoGenerator mpg = new AutoGenerator();

   // 全局配置
   GlobalConfig gc = new GlobalConfig();
   String projectPath = System.getProperty("user.dir");//得到当前工作空间的和项目的目录 
   gc.setOutputDir(projectPath + "/src/main/java");
   gc.setAuthor("gwz");//设置作者
   gc.setOpen(false);//代码生成完成之后是否打开代码所在的文件夹
   mpg.setGlobalConfig(gc);

   // 数据源配置
   DataSourceConfig dsc = new DataSourceConfig();
   dsc.setUrl("jdbc:mysql://localhost:3306/sxtwork?useUnicode=true&useSSL=false&characterEncoding=utf8");
   // dsc.setSchemaName("public");
   dsc.setDriverName("com.mysql.jdbc.Driver");
   dsc.setUsername("root");
   dsc.setPassword("123456");
   mpg.setDataSource(dsc);

   // 包配置
   PackageConfig pc = new PackageConfig();
   pc.setModuleName(scanner("模块名"));//父包里面的子包
   pc.setParent("com.gwz");
   mpg.setPackageInfo(pc);

   // 策略配置
   StrategyConfig strategy = new StrategyConfig();
   strategy.setNaming(NamingStrategy.underline_to_camel);//下划线转驼峰命名
   strategy.setColumnNaming(NamingStrategy.underline_to_camel);//下划线转驼峰命名
   //strategy.setSuperEntityClass("com.sxt.common.BaseEntity");//生成domain类的父类完全限定名
   //strategy.setEntityLombokModel(true);//开启简化get set的方法
   strategy.setRestControllerStyle(true);//开启restController的风格
   strategy.setSuperControllerClass("com.gwz.common.BaseController");
   strategy.setInclude(scanner("表名"));//生成哪些表  sys_user   sys_dept
   //strategy.setSuperEntityColumns("id");//主键
   strategy.setControllerMappingHyphenStyle(true);
   strategy.setTablePrefix("sys_");//生成实体时去掉前缀  
   mpg.setStrategy(strategy);
   mpg.execute();
}

}

